<?php

use Biz\service\springHomeDecActivity\Util;
use Biz\service\springHomeDecActivity\WishService;

class WishAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    const VALIDATOR_TOAST_DEBUG = true;

    //参数校验-规则
    public const VALIDATOR_RULES = [
        'home_pic_id' => 'require',
        'idea' => 'require',
        'product_ids' => 'require',
    ];

    //参数校验-自定义消息
    public const VALIDATOR_MSG = [
        'home_pic_id.require' => '未上传搭建家生成图片',
        'idea.require' => '住家理念、感悟未填写',
        'product_ids.require' => '未选择许愿商品',
    ];

    public function main()
    {
        $lockKey = 'spring_home_dec_act:wish:' . $this->uid;
        Util::lockSubmit($lockKey);

        $homePicId = $this->params['home_pic_id'];
        $idea = $this->params['idea'];
        $productIdStr = $this->params['product_ids'] ?? '';
        $productIds = explode(',', $productIdStr);

        //检查用户是否已许愿
        if (WishService::hasWish($this->uid)) {
            throw new \Exception('error:exsit', self::CODE_ERROR_VIEW_TOSTA);
        }
        
        $nResult = WishService::buildHome($this->uid, $homePicId, $idea, $productIds);
        if (!$nResult) {
            throw new \Exception("很遗憾许愿失败了，请重试！", self::CODE_ERROR_VIEW_TOSTA);
        }
        $this->setSuccess('success');
    }
}